<!DOCTYPE html>
<html>
<head>
<title>Electron Path in Magnetic Field</title>
</head>
<body>
<canvas id="physicsCanvas" width="550" height="480" style="border:1px solid #ccc;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Define parameters for the drawing
    const cx = 250;
    const cy = 240;
    const outerRadius = 160;
    const innerRadius = 120; // This corresponds to radius 'r'

    // Set common drawing styles
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 1;
    ctx.font = '16px sans-serif';

    // 1. Draw the shaded region representing the uniform magnetic field
    ctx.beginPath();
    ctx.arc(cx, cy, outerRadius, 0, 2 * Math.PI);
    ctx.fillStyle = '#E0E0E0'; // Light grey fill
    ctx.fill();
    ctx.strokeStyle = 'black'; // Add a thin border to the grey area
    ctx.stroke();

    // 2. Draw the dashed circular path of the electrons
    ctx.beginPath();
    ctx.setLineDash([7, 5]);
    ctx.lineWidth = 1.5;
    ctx.arc(cx, cy, innerRadius, 0, 2 * Math.PI);
    ctx.stroke();

    // Reset line style to solid for subsequent drawings
    ctx.setLineDash([]);
    ctx.lineWidth = 1;

    // 3. Draw arrows on the path to indicate counter-clockwise motion
    // Helper function to draw a 'V' shaped arrow on the circular path
    function drawArrowOnPath(angle_rad) {
        const size = 12; // Size of the arrow head
        const arrowTipX = cx + innerRadius * Math.cos(angle_rad);
        const arrowTipY = cy + innerRadius * Math.sin(angle_rad);
        
        // The angle of the tangent for counter-clockwise motion
        const tangentAngle = angle_rad + Math.PI / 2;

        // Calculate the two points of the V-shape. The arrow points along the tangent.
        // The two lines of the V-shape are drawn backwards from the tip.
        const p1_angle = tangentAngle - 3 * Math.PI / 4;
        const p1_x = arrowTipX + size * Math.cos(p1_angle);
        const p1_y = arrowTipY + size * Math.sin(p1_angle);

        const p2_angle = tangentAngle + 3 * Math.PI / 4;
        const p2_x = arrowTipX + size * Math.cos(p2_angle);
        const p2_y = arrowTipY + size * Math.sin(p2_angle);

        ctx.beginPath();
        ctx.moveTo(p1_x, p1_y);
        ctx.lineTo(arrowTipX, arrowTipY);
        ctx.lineTo(p2_x, p2_y);
        ctx.lineWidth = 1.5;
        ctx.stroke();
    }

    // Place three arrows at positions similar to the original image
    drawArrowOnPath(-2.3); // ~10:30 position
    drawArrowOnPath(2.3);  // ~7:30 position
    drawArrowOnPath(-0.6); // ~1:30 position

    // 4. Draw the point 'S' on the path
    const s_angle = Math.PI; // 9 o'clock position
    const s_x = cx + innerRadius * Math.cos(s_angle);
    const s_y = cy + innerRadius * Math.sin(s_angle);

    ctx.beginPath();
    ctx.arc(s_x, s_y, 4, 0, 2 * Math.PI);
    ctx.fillStyle = 'black';
    ctx.fill();

    // Label for 'S'
    ctx.font = 'italic 18px sans-serif';
    ctx.textAlign = 'right';
    ctx.textBaseline = 'middle';
    ctx.fillText('S', s_x - 10, s_y);
    ctx.font = '16px sans-serif'; // Reset font to default

    // 5. Draw labels and leader lines
    ctx.textAlign = 'left';
    ctx.textBaseline = 'alphabetic';
    ctx.lineWidth = 1;

    // Label for "region of uniform magnetic field"
    const label1_x = cx + outerRadius + 15;
    const label1_y = cy - 115;
    ctx.fillText('region of uniform', label1_x, label1_y);
    ctx.fillText('magnetic field,', label1_x, label1_y + 20);
    ctx.fillText('flux density B', label1_x, label1_y + 40);

    // Leader line for label 1
    const line1_start_x = label1_x - 5;
    const line1_start_y = label1_y + 2;
    const line1_end_x = cx + outerRadius * Math.cos(-Math.PI / 6);
    const line1_end_y = cy + outerRadius * Math.sin(-Math.PI / 6);
    ctx.beginPath();
    ctx.moveTo(line1_start_x, line1_start_y);
    ctx.lineTo(line1_end_x, line1_end_y);
    ctx.stroke();

    // Label for "path of electrons"
    const label2_x = cx + innerRadius + 25;
    const label2_y = cy + innerRadius + 20;
    ctx.fillText('path of electrons,', label2_x, label2_y);
    ctx.fillText('radius r', label2_x, label2_y + 20);

    // Leader line for label 2
    const line2_start_x = label2_x - 5;
    const line2_start_y = label2_y + 2;
    const line2_end_x = cx + innerRadius * Math.cos(Math.PI / 4);
    const line2_end_y = cy + innerRadius * Math.sin(Math.PI / 4);
    ctx.beginPath();
    ctx.moveTo(line2_start_x, line2_start_y);
    ctx.lineTo(line2_end_x, line2_end_y);
    ctx.stroke();

    // 6. Draw the figure caption below the diagram
    ctx.font = 'bold 16px sans-serif';
    ctx.textAlign = 'center';
    ctx.fillText('Fig. 8.1', cx, cy + outerRadius + 60);

</script>
</body>
</html>